package richbar.com.richbar.activity.me;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.activity.login.RegisterAc;
import richbar.com.richbar.bean.ChangeLoginPwdResult;
import richbar.com.richbar.bean.CodeBean;
import richbar.com.richbar.help.CountDownTimerHelp;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.dialog.TipDialog;

/**
 * Created by QiYang on 2018/11/1.
 */

public class ChangeLoginPwdAc extends BaseActivity {
    @BindView(R.id.original_login_pwd)
    EditText original_login_pwd;
    @BindView(R.id.tel)
    EditText tel_et;
    @BindView(R.id.code)
    EditText code_et;
    @BindView(R.id.get_code)
    TextView get_code;
    @BindView(R.id.new_login_pwd)
    EditText new_login_pwd;
    @BindView(R.id.re_input_pwd)
    EditText re_input_pwd;
    private LoadingDialog loadingDialog;


    @Override
    public int initLayout() {
        return R.layout.ac_change_login_pwd;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("修改登录密码").setRightTv("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit() {
        loadingDialog.show();
        String originalLoginPwd = StringUtil.getStr(original_login_pwd);
        String code = StringUtil.getStr(code_et);
        String tel = StringUtil.getStr(tel_et);
        String newLoginPwd= StringUtil.getStr(new_login_pwd);
        String reInputPwd= StringUtil.getStr(re_input_pwd);
        if (StringUtil.isEmpty(originalLoginPwd,code,newLoginPwd,reInputPwd)){
            ToastUtil.showToast(this,getString(R.string.plz_all));
            return;
        }
        if (!newLoginPwd.equals(reInputPwd)){
            ToastUtil.showToast(this,getString(R.string.not_same));
            return;
        }
        NetWorkUtil.updatePwd(tel, code, originalLoginPwd, newLoginPwd, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                loadingDialog.dismiss();
                final ChangeLoginPwdResult result = MyGson.getInstance().fromJson(data, ChangeLoginPwdResult.class);
                if (checkIsOk(result)){
                    if (result.getData().getUpdatepwd().equals("ok")){
                        showTip();
                    }
                }
            }
        });
    }


    private void showTip() {
        TipDialog.show(ChangeLoginPwdAc.this, "恭喜，登录密码修改成功!", new TipDialog.OnListener() {
            @Override
            public void onBesure() {
                finish();
            }
        });
    }

    private void getCode() {
        if (StringUtil.isEmpty(StringUtil.getStr(tel_et))){
            ToastUtil.showToast(this,getString(R.string.error_tel_number));
            return;
        }
        new CountDownTimerHelp(this, get_code, 60000, 1000).start();
        NetWorkUtil.getRegsinCode(Util.md5(), StringUtil.getStr(tel_et), "5",new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final CodeBean codeBean = MyGson.getInstance().fromJson(data, CodeBean.class);
                ToastUtil.showToast(ChangeLoginPwdAc.this, codeBean.getMsg());
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
            }
        });
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this,0);
    }

    @Override
    public void initData() {

    }

    @OnClick(R.id.get_code)
    public void onViewClicked() {
        getCode();
    }
}
